<?php $this->load->view('header/admin'); ?>
<div id="main">
    <div class="container">
        <div class="span-16 colborder" id="content">
        <?php if (isset($messages)) echo $messages; ?>

                <!-- charts placeholder -->
                <div id="placeholder1" style="height:250px; display: none" class="span-16 last"></div> 
                <div id="placeholder2" style="height:250px; display: none" class="span-16 last"></div>
                <div id="placeholder3" style="height:250px; display: none" class="span-16 last"></div> 

        </div> <!-- end content -->
        <?php $this->load->view('menu/admin')?>
    </div> <!-- end container -->
        <script language="javascript" type="text/javascript"> 
         $(document).ready(function() {
             $('#latest-flights').hide();
             $('#placeholder1').show();
             $('#placeholder2').show();
             $('#placeholder3').show();
             
             $.getJSON('<?php echo site_url(); ?>/profile/stats/ajax_yearstats/<?php echo $person->id;?>', function(data) {
                    var data_count = [];
                    var data_duration = [];
                    var data_years = [];
                    var data_distance = [];
                    var series = [];
                    var years = [];
                    var series_years = [];

                    var colours = ['#1A1F26', '#D9C197', '#B2773F', '#BF4C1A', '#69787F', '#A8C0CC'];

                    $.each(data, function(i, value) {
                        var c = value["flights_count"];
                        var s = value["sum_duration"];
                        var y = value["flight_year"];

                        years[y] = (years[y] == null? 0 : parseInt(years[y])) + parseInt(value["sum_duration"]);
                        series.push(value["flight_date"]);

                        len = series.length;

                        data_count.push(parseInt(value["flights_count"]));
                        data_duration.push((Math.round(value["sum_duration"]/3600)));
                        data_distance.push(parseInt(parseFloat(value["sum_distance"]/1000)));
                    });

                    var idx = 0;
                    for (i in years) {
                        data_years.push({
                            name: i,
                            y: Math.round(years[i] / 3600)//,
                            //color: colours[idx++]
                        });
                        series_years.push(i);
                    }

                    chart = new Highcharts.Chart({
                        chart: {
                          renderTo: 'placeholder1', defaultSeriesType: 'spline'
                        },
                        title: { text: 'Flights / Hours' },
                        legend: { enabled: false },
                        series: [{
                            name: 'Flights', data: data_count, type: 'column',
                            dataLabels: { enabled: true, rotation: -90, x: -3, y: 10, style: { font: 'normal 9px Verdana, sans-serif' }}
                        },{
                            name: 'Distance', data: data_distance,
                            dataLabels: { enabled: true, style: { font: 'normal 9px Verdana, sans-serif' }},
                            yAxis: 1
                        }],
                        xAxis: {
                            categories: series_years,
                            labels: { rotation: -45, align: 'right', x: 10, y: 7, style: { font: 'normal 9px Verdana, sans-serif' } }
                        },
                        yAxis: [{ 
                            title: { text: 'Flights' }
                        }, { 
                            title: { text: 'Distance', margin: 35, style: { color: '#AA4643' } },
                            opposite: true
                        }],
                        tooltip: {
                            formatter: function() {
                                    s = '<b>'+ this.series.name +'</b><br/>'+
                                        this.x  +': '+ ((this.series.name == 'Distance')? Highcharts.numberFormat(this.y, 1) : this.y);
                                return s;
                            }
                        }
                    });

                    chart = new Highcharts.Chart({
                        chart: {
                          renderTo: 'placeholder2', defaultSeriesType: 'spline'
                        },
                        title: { text: 'Flights / Hours' },
                        legend: { enabled: false },
                        series: [{
                            name: 'Flights', data: data_count, type: 'column',
                            dataLabels: { enabled: true, rotation: -90, x: -3, y: 10, style: { font: 'normal 9px Verdana, sans-serif' }}
                        },{
                            name: 'Hours', data: data_duration,
                            dataLabels: { enabled: true, style: { font: 'normal 9px Verdana, sans-serif' }},
                            color: '#89A54E',
                            yAxis: 1
                        }],
                        xAxis: {
                            categories: series_years,
                            labels: { rotation: -45, align: 'right', x: 10, y: 7, style: { font: 'normal 9px Verdana, sans-serif' } }
                        },
                        yAxis: [{ 
                            title: { text: 'Flights' }
                        }, { 
                            title: { text: 'Hours', margin: 35, style: { color: '#89A54E' } },
                            opposite: true
                        }],
                        tooltip: {
                            formatter: function() {
                                return '<b>'+ this.x +'</b><br/>'+ this.y + ' ' + this.series.name;
                            }
                        }
                    });
             });
        });
        </script> 
</div> <!-- end main -->
<?php $this->load->view('footer/standard');?>
